package com.sky.aspect;

import com.sky.Annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

//自定义切面实现字段自动填充
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 指i名切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.Annotation.AutoFill)")
    public void autoFillPointCut(){}
    //前置通知
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始公共字段的自动填充");
        //获得方法签名
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        //获得注解对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        //获得数据库操作类型
        OperationType operationType = autoFill.value();

        Object[] args = joinPoint.getArgs();
        if (args==null||args.length==0){
            return;
        }
        Object arg_1 = args[0];
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();


        if (operationType==OperationType.INSERT){
            Method setCreateTime = null;
            try {
                setCreateTime = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //通过反射赋值
                setCreateTime.invoke(arg_1,now);
                setCreateUser.invoke(arg_1,currentId);
                setUpdateTime.invoke(arg_1,now);
                setUpdateUser.invoke(arg_1,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //通过反射赋值


        }else if (operationType==OperationType.UPDATE){
            Method setUpdateTime = null;
            try {
                setUpdateTime = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg_1.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                setUpdateTime.invoke(arg_1,now);
                setUpdateUser.invoke(arg_1,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }

    }
}
